#pragma indent

using Nemerle.Utility
using Nemerle.Collections
using Nemerle.IO
using System.Text.RegularExpressions

public module SplitCamelCase
    static compBegin: Regex = Regex(@"(?<!^)(?=[A-Z]|_)", RegexOptions.Compiled)

    public Split(input: string, dictionary: Hashtable[string, string]): string
        def takeWord(prefix, rest, components)
            def maybe_longer = match(rest)
                | [] => None()
                | x::xs => takeWord(prefix + x, xs, components+1)
            match(maybe_longer)
                | None() =>
                    if (components == 1)
                        Some((prefix, rest))
                    else if (dictionary.ContainsKey(prefix))
                        Some((dictionary[prefix], rest))
                    else
                        None()
                | x => x
        def convert(words)
            | [] => ""
            | x::xs =>
                def (word, rest) = takeWord(x, xs, 1).UnSome()
                word + " " + convert(rest)
        def words = splitIntoComponents(input).Map(_.ToLower())
        convert(words).Trim()

    splitIntoComponents(input: string): list[string]
        List.FromArray(compBegin.Split(input))

    public First4Components(name: string): string
        def components = splitIntoComponents(name)
        string.Join("", components.FirstN(System.Math.Min(4, components.Length)).ToArray())
        
    public NumberOfComponents(name: string): int
        splitIntoComponents(name).Length
